(function () {
  let leftEffectDom = null;
  let rightEffectDom = null;
  const standDistance = 130;
  const root = document.getElementsByTagName("body")[0];

  let startX = 0,
    startY = 0;
  let endX = 0,
    endY = 0;

  const locationConfig = ['http://172.17.3.30:5501/pre.html','http://172.17.3.30:5501/index.html','http://172.17.3.30:5501/next.html'];
  
  const findPageUrl = (href,to)=>{
    const index =  locationConfig.findIndex((item)=>item === href);
    const config = {
      pre:()=>{
        let curIndex = index - 1;
        if(curIndex< 0){
          curIndex = locationConfig.length - 1;
        }
        return locationConfig[curIndex];
      },
      next:()=>{
        let curIndex = index + 1;
        if(curIndex > locationConfig.length -1){
          curIndex = 0;
        }
        return locationConfig[curIndex];
      }
    }
    return config[to]();
  }

  const toPrePage = () => {
   
    const url = findPageUrl(window.location.href,'pre');
    console.log("toPrePage",url);
    window.location.href = url;
    
  };

  const toNextPage = () => {
    const url = findPageUrl(window.location.href,'next');
    console.log("toNextPage",url);
    window.location.href = url;
  };

  const swiperLeft = (endX, startX) => {
    let distance = endX - startX;
    distance = Math.abs(distance);
 
    rightEffectDom.style.width = `${distance}px`;
    if (distance > standDistance) {
      toNextPage();
    } else {
    }
  };
  const swiperRight = (endX, startX) => {
    const distance = endX - startX;
 
    leftEffectDom.style.width = `${distance}px`;
    if (distance > standDistance) {
      toPrePage();
    } else {
    }
  };
  const swiperRouter = (endX, startX, eventType) => {
    const distance = endX - startX;
    if (distance >= 0) {
      swiperRight(endX, startX);
    }
    if (distance < 0) {
      swiperLeft(endX, startX);
    }

    if ("touchend" === eventType) {
      leftEffectDom.style.width = `${0}px`;
      rightEffectDom.style.width = `${0}px`;
    }
  };

  root.addEventListener("touchstart", (e) => {
    console.log('touchstart')
    const { pageX, pageY } = e.touches[0];
    startX = pageX;
    startY = pageY;
  });

  root.addEventListener("touchmove", (e) => {
    console.log('touchmove')
    const { pageX, pageY } = e.changedTouches[0];
    endX = pageX;
    endY = pageY;
    if (Math.abs(endX - startX) > Math.abs(endY - startY)) {
      swiperRouter(endX, startX);
    }
  });

  root.addEventListener("touchend", (e) => {
    const { pageX, pageY } = e.changedTouches[0];
    endX = pageX;
    endY = pageY;
    swiperRouter(endX, startX, "touchend");
  });

  const getElementByStyleSheets = (styleSheets,strType)=>{
    let objResult  = null;
    for (var i = 0; i < styleSheets.length; i++) {
      var rules = styleSheets[i].cssRules || styleSheets[i].rules;
      for (var j = 0; j < rules.length; j++) {
        if (rules[j].selectorText === strType) {
          objResult = rules[j];
          break;
        }
         
      }
    }
    return objResult;
  }
 
  document.addEventListener("DOMContentLoaded", () => {
    const link = document.createElement('link');
    link.rel = 'stylesheet';
    link.href = './lib/swiper.css'; // 替换为你的 CSS 文件路径
    document.head.appendChild(link);
    link.onload = ()=>{
      
        const effectHeight = `${window.innerHeight}px`;
        var styleSheets = document.styleSheets;
        leftEffectDom = getElementByStyleSheets(styleSheets,"body::before");
        leftEffectDom.style.height = effectHeight;
        rightEffectDom = getElementByStyleSheets(styleSheets,"body::after");
        rightEffectDom.style.height = effectHeight;
   
    }
  });
})();
